<template>
  <div id="boo">
    <div class="header">
      <img :src="list.cover_img" alt="">
      <div class="one">
        <p class="p">{{list.book}}</p>
        <p class="p1">作者：<span>{{list.author}}</span></p>
        <p class="p2">库存：<span>{{list.stock - list.sales_num}}</span></p>
        <div class="one1">
          <div>
            <p class="now-price">
              <span v-if="list.price">{{list.price | toFixPrice}}</span>
              <span v-else>免费</span>
            </p>
            <p class="under-price" v-if="list.underlined_price">
              <span>{{list.underlined_price | toFixPrice}}</span>
            </p>
          </div>

          <p class="ship-price">
            <font>运费：</font>
            <span v-if="list.ship_price">{{list.ship_price | toFixPrice}}元</span>
            <span v-else>免运费</span>
          </p>
        </div>
      </div>
    </div>
    <div class="you" v-if="list.price">
      <p class="p" @click="show=true"><span>活动：</span><span><span class="yy">优惠</span> 领取优惠券最多可减<i>{{max_price | toFixPrice(0)}}</i></span><span>领取
          <van-icon name="arrow" /></span></p>
    </div>
    <div class="nav">
      <van-tabs v-model="active" scrollspy sticky>
        <van-tab title='图书介绍'>
          <div>
            <p class="cd-title">图书介绍</p>
            <div class="cd-details" v-html="list.details"></div>
          </div>
        </van-tab>
        <van-tab title='图书评价'>
          <p class="cd-title">图书评价</p>
          <div>
            <van-empty class="custom-image" image="http://120.53.31.103:86/img/empty.0d284c2e.png" description="暂无评价" />
          </div>
        </van-tab>
      </van-tabs>
    </div>
    <div class="but">
      <button @click="subimt">立即购买</button>
    </div>
    <van-popup v-if="list.price" v-model="show" position="bottom" closeable close-icon="close" round :style="{ height: '60%' }">
      <youlist @getCoupon="getCoupon" :list="list.coupon.all"></youlist>
    </van-popup>
  </div>
</template>
<script>
import { bookdetil, lingyou } from '@/api/user.js'
import youlist from '@/components/youlist'
export default {
  components: { youlist },
  data() {
    return {
      id: this.$route.query.id,
      list: [],
      show: false,
      active: 0
    }
  },
  methods: {
    async getCoupon(i) {
      let { data: data } = await lingyou(this.list.coupon.all[i].id)
      console.log(data)
      if (data.code != 200) {
        this.$toast(data.msg)
      } else {
        this.$toast.success('成功')
        this.show = false
      }
    },
    subimt() {
      if (this.list.stock - this.list.sales_num <= 0) {
        this.$toast({
          message: '你来晚了哦,库存已经没有了~',
          duration: 1000
        })
      } else {
        this.$router.push({
          name: 'shoping',
          query: {
            id: this.id,
            type: 12
          }
        })
      }
    },
    async getdtil() {
      let { data: data } = await bookdetil(this.id)
      console.log(data.data)
      this.list = data.data
    }
  },
  computed: {
    max_price() {
      if (this.list.coupon.all.length) {
        let max_price = 0
        this.list.coupon.all.forEach(item => {
          if (max_price < item.discounted_price) {
            max_price = item.discounted_price
          }
        })
        return max_price
      } else {
        return 0
      }
    }
  },
  created() {
    this.getdtil()
  }
}
</script>
<style lang="scss" scoped>
.nav {
  margin-top: 10px;
  /deep/.van-tabs__content {
    width: 100%;
    .van-tab__pane:nth-child(2) {
      margin: 10px 0;
    }
    .van-tab__pane {
      padding: 15px;
      background: white;
      .cd-details {
        padding: 10px;
        p {
          font-size: 14px;
          margin: 2px 0;
        }
      }
      .van-cell__title {
        font-size: 16px;
      }
      .van-empty {
        padding: 15px 0;
      }
    }
    .cd-title {
      font-size: 15px;
    }
  }
}
#boo {
  background: #f7f8fa;
  padding-bottom: 50px;
}
.header {
  display: flex;
  background: white;
  padding: 25px 15px;
  img {
    width: 100px;
    height: 150px;
    border-radius: 10px;
    box-shadow: 1px 2px 5px 2px #ccc;
    margin-right: 15px;
  }
  .one {
    flex: 1;
    .p {
      font-size: 18px;
    }
    .p1,
    .p2 {
      margin-top: 20px;
      color: #8c8c8c;
      font-size: 12px;
      span {
        color: #595959;
      }
    }
    .one1 {
      display: flex;
      margin-top: 30px;
      justify-content: space-between;
      align-items: center;
      div {
        display: flex;
        align-items: flex-end;
        .now-price {
          color: #ee1f1f;
          font-size: 18px;
        }
        .under-price {
          font-size: 14px;
          color: #979797;
          padding-bottom: 1px;
          margin-left: 2px;
          text-decoration: line-through;
        }
      }
      .ship-price {
        color: #979797;
        font-size: 12px;
      }
    }
  }
}
.you {
  background: white;
  margin-top: 15px;
  padding: 10px 15px;
  .p {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 5px 0;
    span:nth-child(2) {
      flex: 1;
      color: #595959;
      font-size: 12px;
      display: flex;
      align-items: center;
      i {
        color: #ea7a2f;
        font-style: normal;
      }
      .yy {
        background: linear-gradient(90deg, #ceb89c, #a38259);
        padding: 0 5px;
        font-size: 12px;
        color: #262626;
        border-radius: 2px;
        margin-right: 10px;
      }
    }
    span:nth-child(1) {
      color: #8c8c8c;
      font-size: 12px;
    }
    span:nth-child(3) {
      color: #8c8c8c;
      font-size: 12px;
      display: flex;
      align-items: center;
      i {
        font-size: 14px;
      }
    }
  }
}
.but {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  button {
    background: #e60012;
    border: none;
    width: 90%;
    height: 35px;
    border-radius: 25px;
    color: white;
    font-size: 14px;
  }
}
</style>


